package com.dawnling.matissedemo;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import java.io.File;
import java.util.ArrayList;

public class PhotoAdapter extends RecyclerView.Adapter<PhotoAdapter.PhotoViewHolder> {

    public final static int TYPE_ADD = 1;
    public final static int TYPE_PHOTO = 2;
    private ArrayList<String> photoPaths;
    private LayoutInflater inflater;
    private int photoNum = 9;
    private Context mContext;

    public PhotoAdapter(Context mContext, ArrayList<String> photoPaths) {
        this.photoPaths = photoPaths;
        this.mContext = mContext;
        inflater = LayoutInflater.from(mContext);
    }

    /**
     * 动态设置最大张数
     */
    public void initPhotoMax(int max){
        photoNum = max;
    }

    @Override public PhotoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.item_photo_select, parent, false);
        return new PhotoViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final PhotoViewHolder holder, final int position) {
        if (getItemViewType(position) == TYPE_PHOTO) {
            Uri uri = Uri.fromFile(new File(photoPaths.get(position)));
            Glide.with(mContext)
                    .load(uri)
                    .thumbnail(0.1f)
                    .into(holder.ivPhoto);
            holder.ivDelete.setVisibility(View.VISIBLE);
            holder.ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override public void onClick(View view) {
                    if (onItemClickListener != null) onItemClickListener.onDeleteClick(position);
                }
            });
        }else holder.ivDelete.setVisibility(View.GONE);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onItemClickListener != null) onItemClickListener.onItemClick(view, position);
            }
        });
    }

    @Override public int getItemCount() {
        int count = photoPaths.size() + 1;
        if (count > photoNum) count = photoNum;
        return count;
    }

    @Override
    public int getItemViewType(int position) {
        return (position == photoPaths.size() && position != photoNum) ? TYPE_ADD : TYPE_PHOTO;
    }

    public static class PhotoViewHolder extends RecyclerView.ViewHolder {
        private ImageView ivPhoto;
        private ImageView ivDelete;
        public PhotoViewHolder(View itemView) {
            super(itemView);
            ivPhoto = itemView.findViewById(R.id.img);
            ivDelete = itemView.findViewById(R.id.ivDelete);
        }
    }

    //点击 RecyclerView 某条的监听
    public interface OnItemClickListener{
        void onItemClick(View view, int position);
        void onDeleteClick(int position);
    }

    private OnItemClickListener onItemClickListener;
    public void setOnItemClickListener(OnItemClickListener onItemClickListener){
        this.onItemClickListener = onItemClickListener;
    }
}
